/***************************************************
  > Copyright (C) 2024 ==KINGYI== All rights reserved.
  > File Name: guanliclient.c
  > Author:
  > Mail: 
  > Created Time: 2024年09月21日 星期六 16时02分25秒
 ***************************************************/
#include "guanliclient.h"

int main(int argc,const char* argv[])
{
	int sock = 0;
	int ret = 0;
	struct account acc;
	memset(&acc,0,sizeof(acc));
     registers ();
	while(1)
	{
		conservator_client_menu_show();
		sock = create_sock();


		scanf("%d",&acc.type);

		switch(acc.type)
		{
			case 0 :
				break;
			case 1 :

				ret = exam_safety(acc.type,sock);
				if(ret == -1)
				{
					perror("exam_safety");
					exit(-1);
				}
				break;
			case 2 :

				delect_safety(acc.type,sock);
				break;

			case 3 :

				updata_safety(acc.type,sock);
				break;

			case 4 :

				add_safety(acc.type,sock);
				break;

			case 5 :

				alter_leader(acc.type,sock);
				break;

			case 6 :
				all_obtain(acc.type,sock);

				break;

			case 7 :

				add_type(acc.type,sock);          
				break;

			case 8 :
				delet_type(acc.type,sock);               
				break;
			case 9 :
				break;
		}
	}
	return 0;
}

void registers (void)
{
	char *p = NULL;
	p =(char *)malloc(sizeof(char)*10);
	memset(p,0,sizeof(p));
	struct account pt;
	memset(&pt,0,sizeof(struct account));

	while(1) 
	{ 
		printf("-----------------请输入账号-------------\n");
		scanf("%s",pt.account);
		printf("-----------------请输入密码-------------\n");
		scanf("%s",pt.password);

		p = server_password(pt.account,pt.password);
		printf("%s\n",p);
		if(strcmp(p,"success") == 0)
		{
			break;
		}else if(strcmp(p,"error") == 0)
		{
			printf("账号或密码错误，请重新输入\n");
		}
	}

}


void conservator_client_menu_show(void)
{
	printf("--------------------------------查看安全员信息----------------------------1\n");
	printf("--------------------------------删除安全员信息----------------------------2\n");
	printf("--------------------------------修改安全员信息----------------------------3\n");
	printf("--------------------------------增加安全员信息----------------------------4\n");
	printf("--------------------------------修改管理员密码----------------------------5\n");
	printf("--------------------------------查询采集点信息----------------------------6\n");
	printf("--------------------------------增加采集点信息----------------------------7\n");
	printf("--------------------------------删除采集点信息----------------------------8\n");
	printf("-------------------------------------退出---------------------------------0\n");

}

char *server_password(char *account,char *password)
{
     int sock = 0;
	int ret = 0;
	char buf[1024] = {0};
	struct account pt;
	memset(&pt,0,sizeof(struct account));

	struct sockaddr_in serAddr;
	memset(&serAddr,0,sizeof(serAddr));

	sock = socket(AF_INET,SOCK_STREAM,0);
	if(sock == -1)
	{  
		perror("sock");
		exit(-1);
	}

	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(6868);
	serAddr.sin_addr.s_addr = inet_addr("192.168.132.130");

	ret = connect(sock,(struct sockaddr*)&serAddr,sizeof(struct sockaddr));
	if(ret == -1)
	{

		perror("connect");
		exit(-1);
	}


	char *dest = NULL;  
	cJSON *json = NULL;
	cJSON *node = NULL;

	json = cJSON_CreateObject();

	cJSON_AddItemToObject(json,"id",cJSON_CreateString("admin"));
	cJSON_AddItemToObject(json,"account",cJSON_CreateString(pt.account));
	cJSON_AddItemToObject(json,"password",cJSON_CreateString(pt.password));

	dest = cJSON_Print(json);
	strcpy(buf,dest);

	ret = send(sock,buf,sizeof(buf),0);
	if(ret == -1)
	{
		perror("send");
		exit(-1);
	}

	char buf2[1024] = {0};

	ret = recv(sock,buf2,sizeof(buf2),0);
	if(ret == -1)
	{
		perror("recv");
		exit(-1);
	}


	char *p = buf2;
	free(dest);
	cJSON_Delete(json);
	return p;
}
int create_sock(void)
{
	int sock = 0;
	int ret = 0;


	struct sockaddr_in serAddr;
	memset(&serAddr,0,sizeof(serAddr));

	sock = socket(AF_INET,SOCK_STREAM,0);
	if(sock == -1)
	{
		perror("sock");
		exit(-1);
	}

	serAddr.sin_family = AF_INET;
	serAddr.sin_port = htons(6868);
	serAddr.sin_addr.s_addr = inet_addr(ip);

	ret = connect(sock,(struct sockaddr*)&serAddr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("connect");
		exit(-1);
	}

	return sock;
}











